package com.hiquety.csv2sql.tool;

import com.hiquety.csv2sql.modal.Csv;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author hetong
 * @version 1.0
 * @date 2020-05-04
 */
public class CsvSqlTool {

    public static String dropTable(String tableName) {
        return "drop table if exists ".concat(tableName).concat(";");
    }

    public static String createTable(BufferedReader br, String tableName) throws IOException {
        String head = br.readLine();
        String[] headArray = head.split(",");
        if (headArray.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("create table ");
        sb.append(tableName);
        sb.append(" (");
        sb.append("id int unsigned auto_increment,");
        int colNum = 0;
        for (String s : headArray) {
            sb.append(s);
            sb.append(" varchar(255) null comment '");
            sb.append(s);
            sb.append("',");
            colNum++;
        }
        sb.append(" constraint table_name_pk primary key (id))");
        sb.append("comment '临时表' charset = utf8mb4;");
        Csv.colNum = colNum;
        return sb.toString();
    }

    public static String insertTableBatch(BufferedReader br, String tableName) throws IOException {
        String line;
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ");
        sb.append(tableName);
        sb.append(" values ");
        while ((line = br.readLine()) != null) {
            sb.append("(null,");
            String[] split = line.split(",");
            if (split.length == 0) {
                continue;
            }
            int colNum = Csv.colNum;
            for (int i = 0; i < colNum; i++) {
                String s = split[i];
                sb.append("'");
                sb.append(s);
                sb.append("'");
                sb.append(",");
            }
            sb.append("),");
        }
        sb.append(";");
        return sb.toString().replace(",)", ")").replace(",;", ";");
    }

    public static String insertTableByLine(BufferedReader br, String tableName) throws IOException {
        String line;
        StringBuilder sbt = new StringBuilder();
        while ((line = br.readLine()) != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("insert into ");
            sb.append(tableName);
            sb.append(" value (null,");
            String[] split = line.split(",");
            if (split.length == 0) {
                continue;
            }
            int colNum = Csv.colNum;
            for (int i = 0; i < colNum; i++) {
                String s = split[i];
                sb.append("'");
                sb.append(s);
                sb.append("'");
                sb.append(",");
            }
            sb.append(");");
            String replace = sb.toString().replace(",)", ")");
            sbt.append(replace);
            sbt.append("\n");
        }
        return sbt.toString();
    }
}
